package oa.service.user.impl;

import java.util.ArrayList;
import java.util.List;

import oa.dao.user.UserDAO;
import oa.entity.user.User;
import oa.service.user.UserService;
import oa.web.user.vo.UserVo;

public class UserServiceImpl implements UserService {

	private UserDAO userDAO;

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void delete(User user) {
		this.userDAO.removeUser(user);
	}

	public List<UserVo> findAll() {

		List<User> userList = this.userDAO.findAllUsers();

		return posToVos(userList);
	}

	/**
	 * posToVos
	 * 
	 * @param userList
	 * @return
	 */
	private List<UserVo> posToVos(List<User> userList) {
		List<UserVo> result = new ArrayList<UserVo>();
		for (User po : userList) {
			result.add(poToVo(po));
		}
		return result;
	}

	private UserVo poToVo(User po) {
		UserVo result = new UserVo();
		result.setAge(po.getAge());
		result.setFirstname(po.getFirstname());
		result.setId(po.getId());
		result.setLastname(po.getLastname());
		return result;
	}

	public User findById(Integer id) {
		return this.userDAO.findUserById(id);
	}

	public void save(UserVo userVo) {
		User user = voToPo(userVo);
		this.userDAO.saveUser(user);
	}

	/**
	 * VO TO PO
	 * 
	 * @param userVo
	 * @return
	 */
	private User voToPo(UserVo userVo) {
		User user = new User();
		user.setAge(userVo.getAge());
		user.setFirstname(userVo.getFirstname());
		user.setId(userVo.getId());
		user.setLastname(userVo.getLastname());
		return user;
	}

	public void update(User user) {
		this.userDAO.updateUser(user);
	}

}
